package com.dennytech.tac.model;

import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import com.dennytech.tac.net.BaseApi;

public class TravelGuide extends CFObject implements Parcelable {

	int id;
	String name;
	String intro;
	String detail;
	String photoUrl;
	String sPhotoUrl;
	String[] tags;
	String tagsStr;
	Pair[] travel;
	String travelStr;

	public TravelGuide() {
	}

	public TravelGuide(Parcel in) {
		id = in.readInt();
		name = in.readString();
		intro = in.readString();
		detail = in.readString();
		photoUrl = in.readString();
		sPhotoUrl = in.readString();
		tagsStr = in.readString();
		setTags(tagsStr);
		travelStr = in.readString();
		setTravel(travelStr);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public String getPhotoUrl() {
		return photoUrl;
	}

	public void setPhotoUrl(String photoUrl) {
		this.photoUrl = photoUrl;
	}

	public String getSPhotoUrl() {
		return sPhotoUrl;
	}

	public void setSPhotoUrl(String sPhotoUrl) {
		this.sPhotoUrl = sPhotoUrl;
	}

	public String[] getTags() {
		return tags;
	}

	public void setTags(String tagsStr) {
		if (tagsStr == null) {
			return;
		}

		this.tagsStr = tagsStr;
		this.tags = tagsStr.split(";");
	}

	public String getTagsStr() {
		return tagsStr;
	}

	public String getTravelStr() {
		return travelStr;
	}

	public Pair[] getTravel() {
		return travel;
	}

	public void setTravel(String travelStr) {
		if (travelStr == null) {
			return;
		}

		this.travelStr = travelStr;
		String travels[] = travelStr.split("\\|");
		Pair[] travel = new Pair[travels.length / 2 + travels.length % 2];
		for (int i = 0; i < travels.length - 1; i += 2) {
			travel[i / 2] = new Pair(travels[i], travels[i + 1]);
		}
		this.travel = travel;
	}

	public static TravelGuide decode(JSONObject source) {
		TravelGuide guide = null;
		try {
			guide = new TravelGuide();
			guide.setId(source.getInt("id"));
			guide.setName(source.getString("name"));
			guide.setIntro(source.getString("intro"));
			guide.setDetail(source.getString("detail"));
			guide.setTags(source.getString("tagsStr"));
			guide.setTravel(source.getString("travelStr"));

			String photoUrl = source.getString("photourl");
			if (!"".equals(photoUrl)) {
				guide.setPhotoUrl(BaseApi.API_DOMAIN
						+ source.getString("photourl"));

				String[] temp = photoUrl.split("\\.");
				if (temp.length == 2) {
					guide.setSPhotoUrl(BaseApi.API_DOMAIN + temp[0]
							+ "_s." + temp[1]);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return guide;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeString(name);
		dest.writeString(intro);
		dest.writeString(detail);
		dest.writeString(photoUrl);
		dest.writeString(sPhotoUrl);
		dest.writeString(tagsStr);
		dest.writeString(travelStr);
	}

	public static final Parcelable.Creator<TravelGuide> CREATOR = new Parcelable.Creator<TravelGuide>() {
		public TravelGuide createFromParcel(Parcel in) {
			return new TravelGuide(in);
		}

		public TravelGuide[] newArray(int size) {
			return new TravelGuide[size];
		}
	};
}
